﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Uow;
using Abp.EntityFrameworkCore;
using AVISOutBound.SSOServer.Authorization.Members;
using AVISOutBound.SSOServer.EntityFrameworkCore;
using IdentityModel;
using IdentityServer4;
using IdentityServer4.EntityFramework.Mappers;
using IdentityServer4.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;

namespace AVISOutBound.SSOServer
{
    public class DataSeeder
    {
        private readonly ABGUserManager _userManager;
        private readonly IDbContextProvider<SSOServerDbContext> _dbContextProvider;

        public DataSeeder(ABGUserManager userManager, IDbContextProvider<SSOServerDbContext> dbContextProvider)
        {
            _userManager = userManager;
            _dbContextProvider = dbContextProvider;
            
        }
        [UnitOfWork]
        public void SeedData()
        {
            var defaultUser = new Member()
            {
                UserName = "18367300126",
                Name = "test",
                Surname = "test",
                EmailAddress = "18367300126@abg.com",
                NormalizedUserName = "18367300126@ABG.COM",
            };
            var result = _userManager.CreateAsync(defaultUser, "123qwe*").Result;
            if (!result.Succeeded && result.Errors.All(x => x.Description != new IdentityErrorDescriber().DuplicateEmail("18367300126@abg.com").Description))
            {
                throw new Exception("seed failed");
            }
            //using (_unitOfWorkManager.Begin())
            //{
                var dbContext = _dbContextProvider.GetDbContext();
                if (!dbContext.Clients.Any())
                {
                    dbContext.Clients.Add(new Client
                    {
                        ClientId = "AVISOutBound.PCSite",
                        ClientName = "AVISOutBound.PCSite",
                        AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

                        AllowAccessTokensViaBrowser = true,
                        RequireConsent = false,

                        ClientSecrets =
                                {
                                    new Secret("AVISOutBound.PCSite".Sha256())
                                },

                        RedirectUris = { "http://localhost:5001/signin-oidc" },
                        PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

                        AllowedScopes =
                                {
                                    IdentityServerConstants.StandardScopes.OpenId,
                                    IdentityServerConstants.StandardScopes.Profile,
                                    "Avis.OutBoundAPI"
                                },
                    }.ToEntity());

                    dbContext.Clients.Add(new Client
                    {
                        ClientId = "AVISOutBound.MobileSite",
                        ClientName = "AVISOutBound.MobileSite",
                        AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

                        AllowAccessTokensViaBrowser = true,
                        RequireConsent = false,

                        ClientSecrets =
                        {
                            new Secret("AVISOutBound.MobileSite".Sha256())
                        },

                        RedirectUris = {"http://localhost:5002/signin-oidc"},
                        PostLogoutRedirectUris = {"http://localhost:5002/signout-callback-oidc"},

                        AllowedScopes =
                        {
                            IdentityServerConstants.StandardScopes.OpenId,
                            IdentityServerConstants.StandardScopes.Profile,
                            "Avis.OutBoundAPI"
                        },
                    }.ToEntity());
                }

                if (!dbContext.ApiResources.Any())
                {
                    dbContext.ApiResources.Add(new ApiResource()
                    {
                        Scopes = { new Scope("Avis.OutBoundAPI") },
                        Enabled = true,
                        ApiSecrets = { new Secret("Avis.OutBoundAPI".Sha256()) },
                        Description = "Avis.OutBoundAPI",
                        DisplayName = "Avis.OutBoundAPI",
                        Name = "Avis.OutBoundAPI",
                        UserClaims = {  },
                    }.ToEntity());
                }

                if (!dbContext.IdentityResources.Any())
                {
                    dbContext.IdentityResources.AddRange(new IdentityResources.OpenId().ToEntity(),
                        new IdentityResources.Profile().ToEntity(),
                        new IdentityResources.Email().ToEntity());
                }

            //}

        }
    }
}
